﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace RetrieveEntityDetails.Utility
{
    class ApplicationStateManager
    {
        public static void WriteApplicationState(ApplicationStateDO appState)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream file = File.Create(Directory.GetCurrentDirectory() + @"\RED.state");
            formatter.Serialize(file, appState);
            file.Flush();
        }
        public static ApplicationStateDO ReadApplicationState()
        {
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                FileStream file = File.Open(Directory.GetCurrentDirectory() + @"\RED.state", FileMode.Open);
                ApplicationStateDO appState = null;
                lock (file)
                {
                    appState = (ApplicationStateDO)formatter.Deserialize(file);
                    file.Flush();
                }
                return appState;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
    [Serializable]
    class ApplicationStateDO
    {
        public bool isCustomAuthentication;
        public bool isIFD;
        public bool isHTTPS;
        public bool isCRMOnline;
        public string domain;
        public string userName;
        public string serverName;
        public string port;
    }
}

